import { useRequest } from "ahooks";
import { getQuestionListApi } from "@/apis/question";
import { useSearchParams } from "react-router-dom";
import type { searchType } from "@/utils/type";
import { PAGE_PRAMARY_KEY, PAGE_SIZE_PRAMARY_KEY } from "@/constant";

const useLoadQuestionList = (opt:Partial<searchType>) => {
  const [searchParams] = useSearchParams();
  const {isStar=false,isDeleted=false} = opt
  
  const {data,loading,error} =useRequest(async()=>{
    const keyword = searchParams.get("keyword") || "";
    const page = parseInt(searchParams.get(PAGE_PRAMARY_KEY) || "1");
    const pageSize = parseInt(searchParams.get(PAGE_SIZE_PRAMARY_KEY) || "4");
    const data = await getQuestionListApi({keyword,isStar,isDeleted,page,pageSize});

    return data;
  },{
    //刷新依赖项
    refreshDeps: [searchParams]
  })
  return {data,loading,error};
}



export default useLoadQuestionList;